package com.example.administrator.basedemos.activity;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.adapter.MainVpAdapter;
import com.example.administrator.basedemos.base.BaseActivity;

import butterknife.BindView;

import static com.example.administrator.basedemos.R.id;

public class MainActivity extends BaseActivity {
    @BindView(R.id.navigation)
    BottomNavigationView mNavigation;

    @BindView(R.id.main_viewpager)
    ViewPager mMainViewpager;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener;
    private String TAG = "MainActivity";


    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mMainViewpager.setAdapter(new MainVpAdapter(getSupportFragmentManager()));
        mMainViewpager.setCurrentItem(0);
        mNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    @Override
    public void initData() {

    }


    @Override
    public void initListener() {
        super.initListener();
        mMainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                            case 0:
                                    mNavigation.setSelectedItemId(id.navigation_home);
                                break;
                            case 1:
                                    mNavigation.setSelectedItemId(id.navigation_dashboard);
                                break;
                            case 2:
                                    mNavigation.setSelectedItemId(id.navigation_notifications);
                                break;

                            default:
                                break;
                        }
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case id.navigation_home:
                        mMainViewpager.setCurrentItem(0);
                        return true;
                    case id.navigation_dashboard:
                        mMainViewpager.setCurrentItem(1);
                        return true;
                    case id.navigation_notifications:
                        mMainViewpager.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        };
    }

}
